﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Sunny.Entity;
using Sunny.Model.RequestParam;
using Sunny.Util.Exception;

namespace Sunny.Service
{
    public class PostService
    {
        private readonly SunnyContext _sunnyContext;

        public PostService(SunnyContext sunnyContext)
        {
            _sunnyContext = sunnyContext;
        }
        
        public async Task<List<long>> GetPostIdsByUserId(long userId)
        {
            var postIds = await _sunnyContext.SysUserPosts.Where(t=>t.UserId == userId).Select(t=>t.PostId).ToListAsync();
            return postIds;
        }

        public async Task<List<SysPost>> GetAll()
        {
            return await _sunnyContext.SysPosts.ToListAsync();
        }

        public async Task<(List<SysPost>, int)> List(PostListParam param)
        {
            var posts = _sunnyContext.SysPosts.AsQueryable();
            if(!string.IsNullOrEmpty(param.PostCode))
            {
                posts = posts.Where(t=>t.PostCode.Contains(param.PostCode));
            }
            if(!string.IsNullOrEmpty(param.PostName))
            {
                posts = posts.Where(t=>t.PostName.Contains(param.PostName));
            }
            if(!string.IsNullOrEmpty(param.Status))
            {
                posts = posts.Where(t=>t.Status == param.Status);
            }
            int total = await posts.CountAsync();
            var postList = await posts.OrderBy(t=>t.PostSort).Skip((param.PageNum-1)*param.PageSize).Take(param.PageSize).ToListAsync();
            return (postList,total);
        }

        public async Task Add(SysPost sysPost)
        {
            var hasPostCode = await _sunnyContext.SysPosts.AnyAsync(t=>t.PostCode == sysPost.PostCode);
            if(hasPostCode)
            {
                throw new BusinessException(CodeMessage.PostCodeExists);
            }
            var hasPostName = await _sunnyContext.SysPosts.AnyAsync(t=>t.PostName == sysPost.PostName);
            if(hasPostName)
            {
                throw new BusinessException(CodeMessage.PostNameExists);
            }
            await _sunnyContext.SysPosts.AddAsync(sysPost);
            await _sunnyContext.SaveChangesAsync();
        }

        public async Task<SysPost> GetPostById(long postId)
        {
            var post = await _sunnyContext.SysPosts.SingleOrDefaultAsync(t=>t.PostId == postId);
            return post;
        }

        public async Task Update(SysPost sysPost)
        {
            
            var hasPostCode = await _sunnyContext.SysPosts.AnyAsync(t=>t.PostCode == sysPost.PostCode && t.PostId != sysPost.PostId);
            if(hasPostCode)
            {
                throw new BusinessException(CodeMessage.PostCodeExists);
            }
            var hasPostName = await _sunnyContext.SysPosts.AnyAsync(t=>t.PostName == sysPost.PostName&& t.PostId != sysPost.PostId);
            if(hasPostName)
            {
                throw new BusinessException(CodeMessage.PostNameExists);
            }
            var post = await _sunnyContext.SysPosts.SingleOrDefaultAsync(t=>t.PostId == sysPost.PostId);
            post.PostCode = sysPost.PostCode;
            post.PostName = sysPost.PostName;
            post.PostSort = sysPost.PostSort;
            post.Remark = sysPost.Remark;
            await _sunnyContext.SaveChangesAsync();
        }

        public async Task Delete(string postIds)
        {
            string[] postIdArray = postIds.Split(",");
            var pids = Array.ConvertAll(postIdArray,long.Parse);
            foreach (var pid in pids)
            {
                var hasUser = await _sunnyContext.SysUserPosts.AnyAsync(t=>t.PostId == pid);
                if(hasUser)
                {
                    throw new BusinessException(CodeMessage.PostHasUser.SetMessage($"岗位编号【{pid}】已分配用户,不能删除"));
                }
            }

            var sysPosts = _sunnyContext.SysPosts.Where(t=>pids.Contains(t.PostId));
            _sunnyContext.SysPosts.RemoveRange(sysPosts);
            
            await _sunnyContext.SaveChangesAsync();
        }
    }
}